package org.lease.web.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.lease.common.result.Result;
import org.lease.model.dto.CaptchaDTO;
import org.lease.model.dto.LoginDTO;
import org.lease.model.entity.SystemUser;
import org.lease.model.vo.SystemUserInfoVo;
import org.lease.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Tag(name = "登录管理")
@RestController
@RequestMapping("/admin/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Operation(summary = "获取图形验证码")
    @GetMapping("/captcha")
    public Result<CaptchaDTO> getCaptcha() {
        // 调用 Service
        CaptchaDTO captchaDTO = loginService.getCaptcha();
        // 返回响应
        return Result.success(captchaDTO);
    }

    @Operation(summary = "用户登录功")
    @PostMapping("/login")
    public Result<String> login(@Valid @RequestBody LoginDTO loginDTO) {
        log.info("用户登录，请求参数：{}", loginDTO);

        String token = loginService.login(loginDTO);

        log.info("用户登录，token 为：{}", token);
        return Result.success(token);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/info")
    public Result<SystemUserInfoVo> getUserInfo() {
        SystemUser userInfo = loginService.getUserInfo();
        SystemUserInfoVo userInfoVo = new SystemUserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);

        log.info("获取用户信息:{}", userInfoVo);

        return Result.success(userInfoVo);
    }
}
